<?php

namespace app\admin\model\zhongtai;

use think\Model;

class ShoproOrderBill extends Model
{
// 表名
    protected $table = 'shopro_order_bill';
    // 设置当前模型的数据库连接
    protected $connection = 'db_zhongtai';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $deleteTime = false;

    /**
     * 获取最新的更新时间
     */
    public function getUpTime($manage_id)
    {
        $updatetime = $this->where('manage_id', $manage_id)->order("zt_time desc")->value("zt_time");
        return $updatetime ?? 0;
    }

    public function upData($data, $manage_id)
    {
        if ($data) {
            $con = count($data);
            foreach ($data as $k => $v) {
                echo $k . PHP_EOL;
                if (($con - 1) == $k) {
                    $zttime = date('Y-m-d H:i:s',strtotime($v['zt_time'])+1);
                }else{
                    $zttime = $v['zt_time'];
                }
                $user_data = (new User())->getUserStatus($manage_id,$v['user_id']);
                if(!$user_data) continue;
                $in_data = [
                    "manage_id" => $manage_id,
                    "user_id" => $v['user_id'],
                    "bill_id" => $v['id'],
                    "order_sn" => $v['order_sn'],
                    "total_amount" => $v['total_amount'],
                    "fen_money" => $v['fen_money'],
                    "type" => $v['type'],
                    "percent" => $v['percent'],
                    "fz_title" => $v['fz_title'],
                    "fz_money" => $v['fz_money'],
                    "fz_real_money" => $v['fz_real_money'],
                    "wucha_money" => $v['wucha_money'],
                    "fz_type" => $v['fz_type'],
                    "fz_account" => $v['fz_account'],
                    "fz_number" => $v['fz_number'],
                    "status" => $v['status'],
                    "coupon_status" => $v['coupon_status'],
                    "comfirm_status" => $v['comfirm_status'],
                    "checktime" => $v['checktime'],
                    "fz_time" => $v['fz_time'],
                    "goods_type" => $v['goods_type'],
                    "is_pre" => $v['is_pre'],
                    "is_check" => $v['is_check'],
                    "check_res" => $v['check_res'],
                    "ledger_id" => $v['ledger_id'],
                    "isNeedShare" => $v['isNeedShare'],
                    "paytime" => $v['paytime'],
                    "zt_time" => $zttime,
                    "createtime" => $v['createtime'],
                ];
                $user_data = $this->where([
                    'bill_id' =>  $v['id'],
                    'manage_id' => $manage_id,
                ])->find();
                if ($user_data) {
                    $this->where([
                        'bill_id' =>  $v['id'],
                        'manage_id' => $manage_id,
                    ])->update($in_data);
                } else {
                    $this->insert($in_data);
                }
            }
        }
        echo "user表更新完成";
    }
}
